<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/7
 * Time: 12:29
 */

namespace Xcx\Controller;

use Think\Upload;
use Xcx\Common\ErrorCode;
use Xcx\Model\PhotoModel;

class PhotoController extends CommonController
{

    protected $filterArr = array(
        'uploadPhoto'
    );

    public function __construct()
    {
        parent::__construct();
        //验证登陆的操作
        $action = ACTION_NAME;
        if (in_array($action,$this->filterArr)) {
            $this->checkLogin();
        }
    }

    /**
     * 上传照片
     */
    public function uploadPhoto()
    {
        $describe = I('describe');
        $info = $this->uploadImg('photo');
        $photo = $info['photo']['savename'];
        $Photo = new PhotoModel();
        $res = $Photo->uploadPhoto($describe,$photo);
        if ($res) $arr = array('code' => 0,'id'=>$res);
        else $arr = array('code'=>ErrorCode::$Fail,'msg'=>'上传失败');
        $this->ajaxReturn($arr);
    }

    /**
     * 上传
     * @param $filePath
     * @return array|bool
     */
    public function uploadImg($filePath)
    {
        C('UPLOAD_CONFIG.savePath','upload/'.$filePath.'/');
        $config = C('UPLOAD_CONFIG');
        $upload = new Upload($config);
        $info = $upload->upload($_FILES);
        if (!$info) {
            $this->ajaxReturn(array('code'=>ErrorCode::$UploadFail,'msg'=>$upload->getError()));
        } else {
            return $info;
        }
    }

    /**
     * 照片墙列表
     */
    public function getPhotoList ()
    {
        $Photo = new PhotoModel();
        $list = $Photo->getPhotoList();
        $this->ajaxReturn(array('code'=>0,'list'=>$list));
    }

    /**
     * 获取照片墙照片详情
     */
    public function getPhotoDetails()
    {
        $id = I('id');
        $Photo = new PhotoModel();
        $data = $Photo->getPhotoDetails($id);
        if ($data) {
            $arr['code'] = 0;
            $arr['data'] = $data;
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>'信息获取失败');
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 用户点赞
     */
    public function doPraise()
    {
        $id = I('id');
        $Photo = new PhotoModel();
        $data = $Photo->doPraise($id);
        if ($data) {
            $arr['code'] = 0;
        } else {
            $arr = array('code'=>ErrorCode::$Fail,'msg'=>$Photo->getErrMsg());
        }
        $this->ajaxReturn($arr);
    }

}